package com.xd.study.activity;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.xd.study.R;

import java.util.List;

/**
 * Created by DGF on 2015/5/6.
 * 1、使用Gestures Builder生成手势文件
 * 2、加入项目
 * 3、GestureOverlayView
 */
public class GestureOverlayViewActivity extends Activity {

    private GestureLibrary gestureLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture_overlay_view);

        /**
         * 1、找到Gestures Builder生成的手势文件
         * 2、加载手势文件，读取所有手势
         * 3、匹配识别
         */

        GestureOverlayView gestureOverlayView = (GestureOverlayView)findViewById(R.id.gesture_overlay_view);
        ImageView imageView = (ImageView)findViewById(R.id.gesture_overlay_view_image);

        gestureLibrary = GestureLibraries.fromRawResource(
                GestureOverlayViewActivity.this,
                R.raw.gestures);
        gestureLibrary.load();

        gestureOverlayView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {

                //识别手势
                List<Prediction> predictions = gestureLibrary.recognize(gesture);
                Prediction prediction = predictions.get(0);
                if (prediction.score < 3.0){
                    Toast.makeText(GestureOverlayViewActivity.this, "没有该手势", Toast.LENGTH_SHORT).show();
                }else{
                    if (prediction.name.equals("exit")){
                        finish();
                    }else if (prediction.name.equals("next")){
                        Toast.makeText(GestureOverlayViewActivity.this, "下一首", Toast.LENGTH_SHORT).show();
                    }else if (prediction.name.equals("preview")){
                        Toast.makeText(GestureOverlayViewActivity.this, "上一首", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
